package com.sservice.banfu.controller;

import com.sservice.banfu.e.Tpl;
import com.sservice.banfu.service.TplService;
import com.sservice.banfu.vo.TplVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/02/05.
 */
@Controller
@RequestMapping("/admin/tpl/")
public class TplAdminController {

    @Autowired
    TplService tplService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(Tpl tpl) {
        tpl.setCreateAt(new Date());
        tpl.setStatus(2);// 草稿状态
        return new BaseResult(true, String.valueOf(tplService.save(tpl)), 200);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(Tpl tpl) {
        return new BaseResult(true, String.valueOf(tplService.delete(tpl.getId())), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(Tpl tpl) {
        return new BaseResult(true, String.valueOf(tplService.update(tpl)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(Tpl tpl) {
        Tpl tpl1 = tplService.selectById(tpl.getId());
        tpl1.setRemark(tpl1.getContent() != null ? new String(tpl1.getContent()) : "");
        return new BaseResult(true, tpl1);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(TplVo tpl) {
        List<Tpl> list = tplService.queryByPage(tpl);
        list = tplService.reformatContent(list);
        ListResult list1 = new ListResult(true, list);
        list1.setPage(new Pagination(tpl.getSize(), tplService.countByPage(tpl), tpl.getPage()));
        return list1;
    }
}
